﻿using AutoMapper;
using BW.MesProject.Public.ErrorCode;
using BW.MesProject.Public.Infrastructure;
using BW.MesProject.WareHouse.Domain;
using BW.MesProject.WareHouse.Infrastructure;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BW.MesProject.WareHouse.Application
{
    /// <summary>
    /// 人员信息
    /// </summary>
    public class EMPServices : IEMPServices
    {
        private readonly IBaseRepository<EMPModel> _Emp;
        private readonly IMapper _mapper;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="Emp"></param>
        /// <param name="mapper"></param>
        public EMPServices(IBaseRepository<EMPModel> Emp, IMapper mapper)
        {
            _Emp = Emp;
            this._mapper = mapper;
        }


        /// <summary>
        /// 查询人员
        /// </summary>
        /// <param name="WareName"></param>
        /// <returns></returns>
        public ApiResult<List<EMPModel>> GetEMP(string? EmpName, int pagesize, int pageNum)
        {
            try
            {
                int totalCount = 0;
                //查询名称来显示数据
                var list = _Emp.GetAll();
                List<EMPModel> Emplist = new List<EMPModel>();
                if (pagesize == 0)
                {
                    // 不分页，仅按名称筛选
                    if (EmpName != null)
                    {
                        Emplist = list.Where(x => x.EMPName.Contains(EmpName)).OrderBy(u => new { u.CreateTime, u.EMPID }).ToList();
                    }
                    else
                    {
                        Emplist = list.OrderBy(u => new { u.CreateTime, u.EMPID }).ToList();
                    }
                }
                else
                {
                    if (EmpName != null)
                    {
                        Emplist = list.Where(x => x.EMPName.Contains(EmpName)).OrderBy(u => new { u.CreateTime, u.EMPID }).ToPageList(pageNum, pagesize, ref totalCount).ToList();
                    }
                    else
                    {
                        Emplist = list.OrderBy(u => new { u.CreateTime, u.EMPID }).ToPageList(pageNum, pagesize, ref totalCount).ToList();
                    }


                }

                return new ApiResult<List<EMPModel>>()
                {
                    Code = ApiEnums.Success,
                    Message = "查询成功",
                    Data = Emplist.ToList(),
                    TotalCount = totalCount,
                    PageNum = pageNum,
                    PageSize = pagesize
                };
            }
            catch (Exception ex)
            {
                Console.WriteLine($"查询人员信息时发生异常: {ex.Message}");
                return new ApiResult<List<EMPModel>>()
                {
                    Code = ApiEnums.Fail,
                    Message = $"查询失败: {ex.Message}",
                    Data = new List<EMPModel>(),
                    TotalCount = 0,
                    PageNum = pageNum,
                    PageSize = pagesize
                };
            }
            
        }
    }
}
